package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.Customer;
import com.qfedu.openplateform.manager.entity.Recharge;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.service.RechargeService;
import com.qfedu.openplateform.manager.util.GetTime;
import javafx.scene.input.DataFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author LLFZH
 * @date 2021/10/27 16:43
 * @className RechargeController
 * @description ：充值管理控制层
 */
@RestController
public class RechargeController {

    @Autowired
    private RechargeService rechargeService;

    /**
     * 分页查询充值数据
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/sys/recharge/table")
    public TableData findRechargeByPage(@RequestParam(value = "page",required = false,defaultValue = "1")Integer pageNum,
                                        @RequestParam(value = "limit", required = false, defaultValue = "4")Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);

        List<Recharge> rechargeList = rechargeService.findAllRecharge();

        PageInfo<Recharge> rechargePageInfo = new PageInfo<>(rechargeList);


        TableData<Recharge> tableData = new TableData<>();

        List<Recharge> list = rechargePageInfo.getList();

        tableData.setData(list);
        tableData.setCount(rechargePageInfo.getTotal());

        return tableData;

    }

    @RequestMapping("/sys/recharge/del")
    public TableData deleteRechargeById(Integer[] ids) {

        rechargeService.deleteRechargeById(ids);

        TableData<Object> objectTableData = new TableData<>();
        objectTableData.setMsg("删除成功");

        return objectTableData;


    }

    /**
     * 查询所有客户id
     * @return
     */
//    @RequestMapping("sys/customer/tree")
//    public List<Integer> getAllCustomerId() {
//
//        List<Integer> customerList = rechargeService.getAllCustomerId();
//
//
//        return customerList;
//    }

    /**
     * 修改数据
     * @param recharge
     * @return
     */
    @RequestMapping("/sys/recharge/update")
    public TableData updateRecharge(Recharge recharge) throws ParseException {
        System.out.println("recharge = " + recharge);

        recharge.setUpdatetime(GetTime.getTime());
        rechargeService.updateRecharge(recharge);

        TableData<Object> objectTableData = new TableData<>();

        objectTableData.setMsg("修改成功");
        return objectTableData;
    }

    /**
     * 添加数据
     * @param recharge
     * @return
     */
    @RequestMapping("/sys/recharge/add")
    public TableData addRecharge(Recharge recharge) throws ParseException {

//        Long timeMillis = System.currentTimeMillis();
//
//        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
//
//
//        String format = dateFormat.format(new Date(timeMillis));
//
//        dateFormat.parse(format);

        recharge.setCreatetime(GetTime.getTime());
        recharge.setUpdatetime(GetTime.getTime());


        recharge.setOrderId(GetTime.getOrderId());
        rechargeService.addRecharge(recharge);

        TableData<Object> objectTableData = new TableData<>();

        objectTableData.setMsg("修改成功");
        return objectTableData;
    }


}
